﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing;

namespace TravelForwardContracts
{
    public partial class MainForm : Form
    {
        public static String k_db_keyword_company = "Company";
        private String m_connectionString;
        private SqlConnection m_sqlConnection;
        private DataSet m_dsIndividual;
        //private SqlDataAdapter m_sqlDataAdapter;

        private XDatabaseOp m_xDbOp;

        public MainForm()
        {
            InitializeComponent();
            m_xDbOp = new XDatabaseOp();




//           try
//          {
//               m_connectionString =
//                   "Data Source=LITIAN\\SQLEXPRESS;Initial Catalog=TravelexForwardContract;Integrated Security=true";
//               m_sqlConnection = new SqlConnection(m_connectionString);
//               m_dsIndividual = new DataSet();
//           }
//           catch (System.Exception ex)
//           {
//               MessageBox.Show("Exception: " + ex.Message, "Database Exception", 
//               System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
//              this.Close();
//            }
        }
            
        private void MainForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'travelexForwardContractDataSet.allrecords' table. You can move, or remove it, as needed.
            this.allrecordsTableAdapter.Fill(this.travelexForwardContractDataSet.allrecords);

        }
       
        private void buttonClose_Click(object sender, EventArgs e)
        {
            try
            {
				m_xDbOp.closeDb();
                // m_sqlConnection.Close();
                this.Close();
            }
            catch (System.Exception ex)
            {
                 MessageBox.Show("Exception: " + ex.Message, "Database Exception", 
                     System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }

        private void buttonSearchAndPrint_Click(object sender, EventArgs e)
        {
            if (m_sqlConnection != null)
                m_sqlConnection.Close(); 

            try
            {
                //String company = comboBoxCompany.Text.Trim();
                String ClientId = comboBoxClientID.Text.Trim();

                String sqlIndividualRecords = "SELECT Currency, ClientId,Company, AddressLine1, AddressLine2, Attention, FaxNumber,Email, NotificationMethod,RecordDate,DepositCallsIn, DepositCallsOut,CumulativeBalance,Reason FROM allrecords where ClientId='" + ClientId + "'";
                SqlCommand sqlCommand = new SqlCommand(sqlIndividualRecords, m_sqlConnection);
                m_sqlConnection.Open();
                SqlDataReader data_reader = sqlCommand.ExecuteReader();
                DataTable result_table = new DataTable("IndividualReport");
                result_table.Columns.Add("Currency");
                result_table.Columns.Add("ClientId");
                result_table.Columns.Add("Company");
                result_table.Columns.Add("AddressLine1");
                result_table.Columns.Add("AddressLine2");
                result_table.Columns.Add("Attention");
                result_table.Columns.Add("FaxNumber");
                result_table.Columns.Add("Email");
                result_table.Columns.Add("NotificationMethod");
                result_table.Columns.Add("RecordDate");
                result_table.Columns.Add("DepositCallsIn");
                result_table.Columns.Add("DepositCallsOut");
                result_table.Columns.Add("CumulativeBalance");
                result_table.Columns.Add("Reason");

                while (data_reader.Read())
                {
                    DataRow result_row = result_table.NewRow();
                    result_row["Currency"] = data_reader["Currency"].ToString();
                    result_row["ClientId"] = data_reader["ClientId"].ToString();
                    result_row["Company"] = data_reader["Company"].ToString();
                    result_row["AddressLine1"] = data_reader["AddressLine1"].ToString();
                    result_row["AddressLine2"] = data_reader["AddressLine2"].ToString();
                    result_row["Attention"] = data_reader["Attention"].ToString();
                    result_row["FaxNumber"] = data_reader["FaxNumber"].ToString();
                    result_row["Email"] = data_reader["Email"].ToString();
                    result_row["NotificationMethod"] = data_reader["NotificationMethod"].ToString();
                    result_row["RecordDate"] = data_reader["RecordDate"].ToString();
                    result_row["DepositCallsIn"] = data_reader["DepositCallsIn"].ToString();
                    result_row["DepositCallsOut"] = data_reader["DepositCallsOut"].ToString();
                    result_row["CumulativeBalance"] = data_reader["CumulativeBalance"].ToString();
                    result_row["Reason"] = data_reader["Reason"].ToString();
                    result_table.Rows.Add(result_row); 
                }   

                m_dsIndividual.Tables.Add(result_table);
                dataGridViewIndividual.DataSource = m_dsIndividual;
                dataGridViewIndividual.DataMember = "IndividualReport";

                printIndividualReport();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message, "Database Exception",
                     System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }

        private void printIndividualReport()
        {
            PrintPreviewDialog individualPreview = new PrintPreviewDialog();
            individualPreview.Document = this.printDocumentIndividual;
            individualPreview.ShowDialog();
        }

        private void printIndividual_printPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawString("Currency:", new Font("Arial", 14), Brushes.Black, new Point(50,120));
            e.Graphics.DrawString(m_dsIndividual.Tables["IndividualReport"].Rows[1]["Currency"].ToString(), new Font("Arial", 14),
               Brushes.Black, new Point(300, 120));

            e.Graphics.DrawString("Client ID:", new Font("Arial", 14),Brushes.Black, new Point(50, 160));
            e.Graphics.DrawString(m_dsIndividual.Tables["IndividualReport"].Rows[1]["ClientId"].ToString(), new Font("Arial", 14),
   Brushes.Black, new Point(300, 160));

            e.Graphics.DrawString("Client Name:", new Font("Arial", 14),Brushes.Black, new Point(50, 200));
            e.Graphics.DrawString(m_dsIndividual.Tables["IndividualReport"].Rows[1]["Company"].ToString(), new Font("Arial", 14),
   Brushes.Black, new Point(300, 200));

            e.Graphics.DrawString("Address Line1:", new Font("Arial", 14),Brushes.Black, new Point(50, 240));
            e.Graphics.DrawString(m_dsIndividual.Tables["IndividualReport"].Rows[1]["AddressLine1"].ToString(), new Font("Arial", 14),
   Brushes.Black, new Point(300, 240));

            e.Graphics.DrawString("Address Line2:" , new Font("Arial", 14),Brushes.Black, new Point(50, 280));
            e.Graphics.DrawString(m_dsIndividual.Tables["IndividualReport"].Rows[1]["AddressLine2"].ToString(), new Font("Arial", 14),
   Brushes.Black, new Point(300, 280));

            e.Graphics.DrawString("Fax Number:", new Font("Arial", 14), Brushes.Black, new Point(50, 320));
            e.Graphics.DrawString(m_dsIndividual.Tables["IndividualReport"].Rows[1]["FaxNumber"].ToString(), new Font("Arial", 14),
Brushes.Black, new Point(300, 320));

            e.Graphics.DrawString("Email:", new Font("Arial", 14),Brushes.Black, new Point(50, 360));
            e.Graphics.DrawString(m_dsIndividual.Tables["IndividualReport"].Rows[1]["Email"].ToString(), new Font("Arial", 14),
Brushes.Black, new Point(300, 360));

            e.Graphics.DrawString("Method of Notification :", new Font("Arial", 14),Brushes.Black, new Point(50, 400));
            e.Graphics.DrawString(m_dsIndividual.Tables["IndividualReport"].Rows[1]["NotificationMethod"].ToString(), new Font("Arial", 14),
Brushes.Black, new Point(300, 400));
        }

        private void allReportsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Colse Database Connection
            try
            {
                m_sqlConnection.Close();

                AllReportsForm formAllReport = new AllReportsForm();
                formAllReport.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message, "Close Exception",
                     System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }

    }
}
